< previous page page_140 next page >

Page 140
A dialog box is a window used to display and/or accept information. Its name comes from the fact that it is, in essence, a dialog (or conversation) with the user. A dialog box is usually shown modally, which means the user must close it (or "answer the dialog") before continuing with any other part of the program. In this chapter, you'll look at two dialog boxes built in to Visual Basic: the message box and the input box. Next, you'll use the CommonDialog custom control, which allows you to place four types of standard dialog boxes in your program. Finally, you'll review some guidelines for creating your own form-based dialog box.
Keeping the User Informed
A big part of any programming project is providing information to the users about the program's progress and status. Although the forms and controls of your program provide the main interface to the users, they are not necessarily the best vehicles for providing bits of information that require immediate attention, such as warnings or error messages. For providing this type of information, the message box is the way to go.
Understanding the Message Box
The message box is a simple form that displays a message and at least one command button. The button is used to acknowledge the message and close the form. Because message boxes are built in to the Visual Basic language, you do not have to worry about creating or showing a form. To see a simple message box, type the following line of code into Visual Basic's Immediate window (choose View, Immediate Window to open the window) and press Enter:
MsgBox "I love Visual Basic!"
Optionally, the message box can display an icon or use multiple buttons to let the user make a decision. This is done with the use of optional parameters. For example, the following code line produces the message box shown in Figure 7.1:
MsgBox "Delete record?", vbYesNo + vbExclamation, "Confirm Delete"
note.gif You can try this example yourself. Just type the code in the Immediate window and press Enter. This action has the same effect as if the statement were executed at runtime. square.gif

0140-01.jpg
Figure 7.1
A message box communicates with the user.
Message boxes can be used in either of two ways, depending on your needs. You can use the message box to simply display information, or you can use it to get a decision from the user. In either case, you will use some form of the MsgBox function.

 
< previous page page_140 next page >